package com.pzb.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.pzb.common.utils.DateUtils;
import com.pzb.goods.domain.GdFreightTemplateData;
import com.pzb.goods.service.IGdFreightTemplateDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pzb.goods.mapper.GdFreightTemplateMapper;
import com.pzb.goods.domain.GdFreightTemplate;
import com.pzb.goods.service.IGdFreightTemplateService;

/**
 * 运费模板Service业务层处理
 * 
 * @author Mr_Wu
 * @date 2021-12-06
 */
@Service
public class GdFreightTemplateServiceImpl implements IGdFreightTemplateService 
{
    @Autowired
    private GdFreightTemplateMapper gdFreightTemplateMapper;
    @Autowired
    private IGdFreightTemplateDataService freightTemplateDataService;

    /**
     * 查询运费模板
     * 
     * @param id 运费模板主键
     * @return 运费模板
     */
    @Override
    public GdFreightTemplate selectGdFreightTemplateById(Long id)
    {
        GdFreightTemplate freightTemplate = gdFreightTemplateMapper.selectGdFreightTemplateById(id);
        GdFreightTemplateData q = new GdFreightTemplateData();
        q.setTemplateId(id);
        List<GdFreightTemplateData> dataList = freightTemplateDataService.selectGdFreightTemplateDataList(q);
        List<String> freeCiteList=new ArrayList<>();
        List<GdFreightTemplateData> freightTemplateDataList=new ArrayList<>();
        for (GdFreightTemplateData templateData : dataList) {
            if("0".equals(templateData.getType())){
                freeCiteList.add(templateData.getProvinceCode());
            }else if("1".equals(templateData.getType())){
                freightTemplateDataList.add(templateData);
            }
        }
        freightTemplate.setFreeCityList(freeCiteList);
        freightTemplate.setTemplateDataList(freightTemplateDataList);
        return freightTemplate;
    }

    /**
     * 查询运费模板列表
     * 
     * @param gdFreightTemplate 运费模板
     * @return 运费模板
     */
    @Override
    public List<GdFreightTemplate> selectGdFreightTemplateList(GdFreightTemplate gdFreightTemplate)
    {
        return gdFreightTemplateMapper.selectGdFreightTemplateList(gdFreightTemplate);
    }

    /**
     * 新增运费模板
     * 
     * @param gdFreightTemplate 运费模板
     * @return 结果
     */
    @Override
    public int insertGdFreightTemplate(GdFreightTemplate gdFreightTemplate)
    {
        int count =  gdFreightTemplateMapper.insertGdFreightTemplate(gdFreightTemplate);
        if (count>0) {
            //包邮区域
            for (String cityCode : gdFreightTemplate.getFreeCityList()) {
                GdFreightTemplateData templateData = new GdFreightTemplateData();
                templateData.setExtAppid(gdFreightTemplate.getExtAppid());
                templateData.setTemplateId(gdFreightTemplate.getId());
                templateData.setProvinceCode(cityCode);
                templateData.setType("0");
                freightTemplateDataService.insertGdFreightTemplateData(templateData);
            }
            //非包邮区域
            for (GdFreightTemplateData templateData : gdFreightTemplate.getTemplateDataList()) {
                templateData.setExtAppid(gdFreightTemplate.getExtAppid());
                templateData.setTemplateId(gdFreightTemplate.getId());
                freightTemplateDataService.insertGdFreightTemplateData(templateData);
            }
        }
        return count;
    }

    /**
     * 修改运费模板
     * 
     * @param gdFreightTemplate 运费模板
     * @return 结果
     */
    @Override
    public int updateGdFreightTemplate(GdFreightTemplate gdFreightTemplate)
    {
        gdFreightTemplate.setUpdateTime(DateUtils.getNowDate());
        //先删除
        freightTemplateDataService.deleteGdFreightTemplateDataByTemplateId(gdFreightTemplate.getId());
        //后新增
        //包邮区域
        for (String cityCode : gdFreightTemplate.getFreeCityList()) {
            GdFreightTemplateData templateData = new GdFreightTemplateData();
            templateData.setExtAppid(gdFreightTemplate.getExtAppid());
            templateData.setTemplateId(gdFreightTemplate.getId());
            templateData.setProvinceCode(cityCode);
            templateData.setType("0");
            freightTemplateDataService.insertGdFreightTemplateData(templateData);
        }
        //非包邮区域
        for (GdFreightTemplateData templateData : gdFreightTemplate.getTemplateDataList()) {
            templateData.setExtAppid(gdFreightTemplate.getExtAppid());
            templateData.setTemplateId(gdFreightTemplate.getId());
            freightTemplateDataService.insertGdFreightTemplateData(templateData);
        }
        return gdFreightTemplateMapper.updateGdFreightTemplate(gdFreightTemplate);
    }

    /**
     * 删除运费模板信息
     * 
     * @param id 运费模板主键
     * @return 结果
     */
    @Override
    public int deleteGdFreightTemplateById(Long id)
    {
        return gdFreightTemplateMapper.deleteGdFreightTemplateById(id);
    }

    @Override
    public boolean checkTemplateExistGoods(Long id) {

        int count = gdFreightTemplateMapper.checkTemplateExistGoods(id);
        return count > 0 ? true : false;
    }
}
